package com.example.exam4.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.exam4.R;
import com.example.exam4.entity.Planet;

import java.util.List;

/**
 * 适配器
 */
public class PlanetAdapter extends RecyclerView.Adapter<PlanetAdapter.ViewHolder> {
    private List<Planet> planetList;

    public PlanetAdapter(List<Planet> planetList) {
        this.planetList = planetList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_planet, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Planet planet = planetList.get(position);
        holder.plaImg.setImageResource(planet.getPlaImg());
        holder.plaTitle.setText(planet.getPlaTitle());
        holder.plaDesc.setText(planet.getPlaDesc());
    }

    @Override
    public int getItemCount() {
        return planetList.size();
    }

    /**
     * 实现ViewHolder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView plaImg;
        TextView plaTitle;
        TextView plaDesc;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            //可以使用glide加载
            plaImg = itemView.findViewById(R.id.planet_img);
            plaTitle = itemView.findViewById(R.id.planet_title);
            plaDesc = itemView.findViewById(R.id.planet_desc);
        }
    }
}
